from src.utils.analysis import Judge
import numpy as np

def read_result(txt_path):
    result_list = []
    with open(txt_path) as f:
        for line in f.readlines():
            line = line.replace('\n', '')
            result_list.append(int(line))
    return result_list

def model_quality(original_label_list, result_label_list, label_type):
    label_list = np.arange(0, label_type, 1).tolist()
    label_type_num = len(label_list)

    accuracy, precision, recall = 0,0,0

    jg = Judge(original_label_list, result_label_list)

    print('*********模型分析开始***********')
    for i in label_list:
        print('标签%d的统计结果：precision:%f, recall:%f' % (i, jg.precision_for_HER2(i), jg.recall_for_HER2(i)))
        precision += jg.precision_for_HER2(i)
        recall += jg.recall_for_HER2(i)

    accuracy = jg.accuracy_for_HER2()
    precision = precision / label_type_num
    recall = recall / label_type_num

    print('总的统计结果：accuracy:%f, precision:%f, recall:%f' % (accuracy, precision, recall))

    print('*********模型分析结束***********')

if __name__ == '__main__':
    original_label_txt = '../../data/txt/model_result/Multi_task_original_magnification_label.txt'
    result_label_txt = '../../data/txt/model_result/Multi_task_result_magnification_label.txt'
    original_label_list = read_result(original_label_txt)
    result_label_list = read_result(result_label_txt)

    label_type = 3  # 标签种类
    model_quality(original_label_list,result_label_list,label_type)
